
<table border="1">
<tr valign="top"><td>Actifs<table>
<?
$sql="SELECT no, libelle_admin FROM cpt_comptes WHERE groupe='1' AND no LIKE '1%' ORDER BY SUBSTRING(`no`,1),SUBSTRING(`no`,2),SUBSTRING(`no`,3),SUBSTRING(`no`,4),SUBSTRING(`no`,5),SUBSTRING(`no`,6),SUBSTRING(`no`,7),SUBSTRING(`no`,8),SUBSTRING(`no`,9)";
$res=mysql_query($sql,$db);
while($ligne=mysql_fetch_assoc($res))
	{
	print '<tr><td>'.$ligne['no'].'</td><td><a href="?tt_compta=1&cpts=no%20LIKE%20%27'.$ligne['no'].'%25%27">'.$ligne['libelle_admin'].'</a></td></tr>';
	
	}
?>
</table><hr><a href="?tt_compta=1&cpts=no%20LIKE%20%271%25%27%27">				</a><a href="?tt_compta=1&cpts=no%20LIKE%20%271%25%27%20AND%20no%20NOT%20LIKE%20%27111%25%27%20AND%20no%20NOT%20LIKE%20%27151%25%27">		groupe 1 (sans matos)<br>
				</a>
				<a href="?tt_compta=1&cpts=no%20LIKE%20%27100%25%27%20OR%20no%20LIKE%20%27101%25%27">groupe
				10-0/1 (caisses)</a></td><td>Passifs<table>
<?
$sql="SELECT no, libelle_admin FROM cpt_comptes WHERE groupe='1' AND no LIKE '2%' ORDER BY  SUBSTRING(`no`,1),SUBSTRING(`no`,2),SUBSTRING(`no`,3),SUBSTRING(`no`,4),SUBSTRING(`no`,5),SUBSTRING(`no`,6),SUBSTRING(`no`,7),SUBSTRING(`no`,8),SUBSTRING(`no`,9)";
$res=mysql_query($sql,$db);
while($ligne=mysql_fetch_assoc($res))
	{
	print '<tr><td>'.$ligne['no'].'</td><td><a href="?tt_compta=1&cpts=no%20LIKE%20%27'.$ligne['no'].'%25%27">'.$ligne['libelle_admin'].'</a></td></tr>';
	
	}
?>
</table></td><td>Charges<table>
<?
$sql="SELECT no, libelle_admin FROM cpt_comptes WHERE groupe='1'  AND (no LIKE '3%' OR no LIKE '4%') ORDER BY SUBSTRING(`no`,1),SUBSTRING(`no`,2),SUBSTRING(`no`,3),SUBSTRING(`no`,4),SUBSTRING(`no`,5),SUBSTRING(`no`,6),SUBSTRING(`no`,7),SUBSTRING(`no`,8),SUBSTRING(`no`,9)";
$res=mysql_query($sql,$db);
while($ligne=mysql_fetch_assoc($res))
	{
	print '<tr><td>'.$ligne['no'].'</td><td><a href="?tt_compta=1&cpts=no%20LIKE%20%27'.$ligne['no'].'%25%27">'.$ligne['libelle_admin'].'</a></td></tr>';
	
	}
?>
</table>
</td>
<td>Produits<table>
<?
$sql="SELECT no, libelle_admin FROM cpt_comptes WHERE groupe='1'  AND no LIKE '6%' ORDER BY SUBSTRING(`no`,1),SUBSTRING(`no`,2),SUBSTRING(`no`,3),SUBSTRING(`no`,4),SUBSTRING(`no`,5),SUBSTRING(`no`,6),SUBSTRING(`no`,7),SUBSTRING(`no`,8),SUBSTRING(`no`,9)";
$res=mysql_query($sql,$db);
while($ligne=mysql_fetch_assoc($res))
	{
	print '<tr><td>'.$ligne['no'].'</td><td><a href="?tt_compta=1&cpts=no%20LIKE%20%27'.$ligne['no'].'%25%27">'.$ligne['libelle_admin'].'</a></td></tr>';
	
	}
?>
</table></td></tr>
</table>
<form action="" method="post">

<?
//$_SESSION['interval_cpt_debut']="";
//$_SESSION['interval_cpt_fin']="";
if ($_POST['Submit'])
	{
	//print_r($_SESSION);
	//print_r($_POST);
	$_SESSION['interval_cpt_debut']=$_POST['interval_cpt_debut_Y']."-".$_POST['interval_cpt_debut_m']."-".$_POST['interval_cpt_debut_d'];
	$_SESSION['interval_cpt_fin']=$_POST['interval_cpt_fin_Y']."-".$_POST['interval_cpt_fin_m']."-".$_POST['interval_cpt_fin_d'];
	$_SESSION['interval_act']=$_POST['interval'];
	}
if (!$_SESSION['interval_cpt_debut'])
	{$_SESSION['interval_cpt_debut']=date("Y-m")."-01";}
if (!$_SESSION['interval_cpt_fin'] )
	{
	$next_m=date("m")+1;
	if ($next_m==13)
	{$next_m=1;}
	if ($next_m<10)
	{$next_m="0".$next_m;}
	
	$_SESSION['interval_cpt_fin']=date("Y-").$next_m."-01";
	} 
?>
Interval 
		<input name="interval" type="checkbox" id="interval" value="1" <? if ($_SESSION['interval_act']) {print "checked";} ?>>
		du
		<?
dates_select_trois($_SESSION['interval_cpt_debut'], "2000", (date("Y")+50), "interval_cpt_debut");
print "(incl) au ";
dates_select_trois($_SESSION['interval_cpt_fin'], "2000", (date("Y")+50), "interval_cpt_fin");
print " (excl) ";


?>
<input type="submit" name="Submit" value="OK">
</form>

<?
$groupe_sous_prec=FALSE;
$sql_comptes="SELECT no, libelle_admin, commentaire, groupe_sous FROM cpt_comptes WHERE ( ".$_GET['cpts']." ) AND groupe='0' ORDER BY SUBSTRING(`no`,1),SUBSTRING(`no`,2),SUBSTRING(`no`,3),SUBSTRING(`no`,4),SUBSTRING(`no`,5),SUBSTRING(`no`,6),SUBSTRING(`no`,7),SUBSTRING(`no`,8),SUBSTRING(`no`,9)";

$sql_comptes=stripslashes($sql_comptes);

print "<table border=0><tr><td><strong>grp</strong></td><td><strong>cpt</strong></td><td><strong>libellé</strong></td><td><strong>solde</strong></td></tr>";
$res_comptes=mysql_query($sql_comptes,$db);
while($ligne_comptes=mysql_fetch_object($res_comptes))
	{
	
	
	if ($_SESSION['interval_act'])
		{
		//$sql_journal="SELECT * FROM cpt_journal WHERE ( no_compte_credit='$ligne_comptes->no' OR no_compte_debit='$ligne_comptes->no' ) AND date >= '".$_SESSION['interval_cpt_debut']." 00:00:00' AND date < '".$_SESSION['interval_cpt_fin']." 00:00:00'";
		$sql_journal_credit="SELECT sum(somme) as total_credit FROM cpt_journal WHERE no_compte_credit='$ligne_comptes->no' AND date >= '".$_SESSION['interval_cpt_debut']." 00:00:00' AND date < '".$_SESSION['interval_cpt_fin']." 00:00:00'";
		$sql_journal_debit="SELECT sum(somme) as total_debit FROM cpt_journal WHERE no_compte_debit='$ligne_comptes->no' AND date >= '".$_SESSION['interval_cpt_debut']." 00:00:00' AND date < '".$_SESSION['interval_cpt_fin']." 00:00:00'";

		}
	else
		{
		//$sql_journal="SELECT no_compte_debit, no_compte_credit, somme FROM cpt_journal WHERE (no_compte_credit='$ligne_comptes->no' OR no_compte_debit='$ligne_comptes->no') AND date <= NOW()";
		$sql_journal_credit="SELECT sum(somme) as total_credit FROM cpt_journal WHERE no_compte_credit='$ligne_comptes->no' AND date <= NOW()";
		$sql_journal_debit="SELECT sum(somme) as total_debit FROM cpt_journal WHERE no_compte_debit='$ligne_comptes->no' AND date <= NOW()";
		}
	$res_journal_credit=mysql_query($sql_journal_credit,$db);
	$res_journal_debit=mysql_query($sql_journal_debit,$db);
	$ligne_journal_credit=mysql_fetch_assoc($res_journal_credit);
	$ligne_journal_debit=mysql_fetch_assoc($res_journal_debit);
	$total_credit=$ligne_journal_credit['total_credit'];
	$total_debit=$ligne_journal_debit['total_debit'];
	
	$sql_verrou="SELECT type FROM cpt_verrou WHERE no_cpt_comptes='$ligne_comptes->no'";
	$res_verrou=mysql_query($sql_verrou,$db);
	$ligne_verrou=mysql_fetch_assoc($res_verrou);

	$solde_cpt[$ligne_comptes->no]=round($total_debit-$total_credit,2);
	if (substr($ligne_comptes->no,0,1)==2 OR substr($ligne_comptes->no,0,1)==6)
		{$solde_cpt[$ligne_comptes->no]=-$solde_cpt[$ligne_comptes->no];}
	$total_credit=0;
	$total_debit=0;
	$total=$total+$solde_cpt[$ligne_comptes->no];
	
	
	
	print "<tr><td>";
	if ($groupe_sous_prec!=$ligne_comptes->groupe_sous)
		{print "<a href=\"?tt_compta=1&cpts=no%20LIKE%20%27".$ligne_comptes->groupe_sous."%25%27\">".$ligne_comptes->groupe_sous."</a>";}
	$groupe_sous_prec=$ligne_comptes->groupe_sous;
	print "</td><td><a href=?compta_compte=$ligne_comptes->no>$ligne_comptes->no</a></td><td width=300><a Title=\"$ligne_comptes->commentaire\">$ligne_comptes->libelle_admin</a> ";
	if($ligne_verrou['type']=="total")
		{ print '<img src="../images/2.gif" width="10">';}
	elseif($ligne_verrou['type'])
		{ print '';}
	print "</td><td>".$solde_cpt[$ligne_comptes->no]." </td></tr>";
	}
	print "<tr><td></td><td></td><td align=right><strong>TOTAL</strong></td><td><strong>$total</strong></td></tr>";
	print "</table>";
?>
</p>
